home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / TUTORC.ZIP / TUT13.C < prev    next >
C/C++ Source or Header  |  1994-10-30  |  6KB  |  216 lines

  1. /* 
  2.   tut13.c
  3.   10/30/94
  4.   from tutprog13.pas
  5.   Adapted from Denthor's tutprog13.pas
  6.   Translated into C, from Denthor's VGA Trainer, by
  7.   Steve Pinault, scp@ohm.att.com
  8.   Compiled with Microsoft Visual C++ 1.5 (Microsoft C 8.0)
  9.   To compile:
  10.   First compile the subroutines in tutsubs.c with the batch file 
  11.   cltutsub.bat
  12.   Then compile any of the tutor programs with the batch file
  13.   cltut.bat
  14.   Example: C:>cltutsub
  15.            C:>cltut tut13.c
  16.            to compile this program.
  17. */
  18.  
  19. #include "tutheadr.h"
  20.  
  21. #define NUM 400     // Number of stars
  22.  
  23. struct Star
  24. {
  25.   int x;
  26.   int y;
  27.   int z;
  28. } Stars[NUM];
  29.  
  30. struct Pos
  31. {
  32.   int x;
  33.   int y;
  34. } Clear[2][NUM];
  35.  
  36. char logo[50][320];
  37. char far* logoptr=&logo[0][0];
  38.  
  39. // {DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD}
  40. // Procedure Init;
  41. void Init()
  42. {
  43.   int loop1,loop2;
  44.  
  45.   for(loop1=0;loop1<NUM;loop1++)  
  46.     do
  47.     {
  48.       //Stars[loop1].x=random (320)-160;
  49.       //Stars[loop1].y=random (200)-100;
  50.       Stars[loop1].x=random (600)-300; //Fill out the distant field more
  51.       Stars[loop1].y=random (400)-200;
  52.       Stars[loop1].z=loop1+1; 
  53.     } while((Stars[loop1].x==0)&&(Stars[loop1].y==0));
  54.     //  { Make sure no stars are heading directly towards the viewer }
  55.     
  56.   for(loop1=0;loop1<32;loop1++)
  57.     Pal ((unsigned char)(32+loop1),(unsigned char)(10+loop1),(unsigned char)(10+loop1),(unsigned char)(30+loop1));
  58.  
  59.   Pal ((unsigned char)247,20,20,20);
  60.   Pal ((unsigned char)136,30,0 ,0 );
  61.   Pal (101,40,0 ,0 );
  62.   Pal (19 ,60,0 ,0 ); // { Pallette for the logo at the top of the screen }
  63.  
  64.   LoadCEL ("logo.cel",logoptr);
  65.   for(loop1=0;loop1<320;loop1++)
  66.     for(loop2=0;loop2<50;loop2++)
  67.       PutPixel (loop1,loop2,logo[loop2][loop1],VGA);
  68.       // { Placing the logo at the top of the screen }
  69. }
  70.  
  71. // {DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD}
  72. // Procedure Calcstars;
  73. //  { This ccalculates the 2-d coordinates of our stars and saves these values
  74. //    into the variable clear }
  75. void Calcstars()
  76. {
  77.   int loop1,x,y;
  78.   for(loop1=0;loop1<NUM;loop1++)
  79.   {
  80.     x=((Stars[loop1].x << 7) / Stars[loop1].z)+160;
  81.     y=((Stars[loop1].y << 7) / Stars[loop1].z)+100;
  82.     Clear[0][loop1].x=x;
  83.     Clear[0][loop1].y=y;
  84.   }
  85. }
  86. // {DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD}
  87. // Procedure Drawstars;
  88. //  { This draws the 2-d values stored in clear to the vga screen, with various
  89. //    colors according to how far away it is. }
  90. void Drawstars()
  91. {
  92.   int loop1,x,y;
  93.   int starcolor;
  94.   for(loop1=0;loop1<NUM;loop1++)
  95.   {
  96.     x=Clear[0][loop1].x;
  97.     y=Clear[0][loop1].y;
  98.     if((x>0) && (x<320) && (y>50) && (y<200))
  99.     {
  100.       if(Stars[loop1].z>383)
  101.         starcolor=32;
  102.       else
  103.         starcolor=63-(Stars[loop1].z/12);
  104.       PutPixel(x,y,(unsigned char)starcolor,VGA);
  105.     }
  106.   }
  107. }  
  108.  
  109. // {DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD}
  110. // Procedure Clearstars;
  111. //  { This clears the 2-d values from the vga screen, which is faster then a
  112. //    cls (vga,0) }
  113. void Clearstars()
  114. {
  115.   int loop1,x,y;
  116.   for(loop1=0;loop1<NUM;loop1++)
  117.   {
  118.     x=Clear[1][loop1].x;
  119.     y=Clear[1][loop1].y;
  120.     if((x>0)&&(x<320)&&(y>50)&&(y<200))PutPixel(x,y,0,VGA);
  121.   }
  122. }
  123.  
  124. // {DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD}
  125. // Procedure MoveStars (Towards:boolean);
  126. //  { If towards is True, then the z-value of each star is decreased to come
  127. //    towards the viewer, otherwise the z-value is increased to go away from
  128. //    the viewer }
  129.  
  130. void Movestars(int towards)
  131. {
  132.   int loop1;
  133.   if(towards) for(loop1=0;loop1<NUM;loop1++)
  134.   {
  135.       Stars[loop1].z=Stars[loop1].z-2;
  136.       if(Stars[loop1].z<1)Stars[loop1].z=Stars[loop1].z+NUM;
  137.   }
  138.   else for( loop1=0;loop1<NUM;loop1++)
  139.   {
  140.       Stars[loop1].z=Stars[loop1].z+2;
  141.       if(Stars[loop1].z>NUM)Stars[loop1].z=Stars[loop1].z-NUM;
  142.   }
  143. }
  144.  
  145. // {DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD}
  146. // Procedure Play;
  147. //  { This is our main procedure }
  148. void Play()
  149. {
  150.   int ch;
  151.   Calcstars();
  152.   Drawstars(); // { This draws our stars for the first time }
  153.   do
  154.   { 
  155.     if(_bios_keybrd(_KEYBRD_READY)) ch=getch()&0x00ff;
  156.     _fmemmove(Clear[1],Clear[0],4*NUM);
  157.     Calcstars();   //  { Calculate new star positions }
  158.     WaitRetrace();
  159.     Clearstars();  //  { Erase old stars }
  160.     Drawstars();     //  { Draw new stars }
  161.     if(ch==' ')Movestars(FALSE); else Movestars(TRUE);
  162.       //{ Move stars towards or away from the viewer }
  163.   }
  164.   while(ch!=0x1b); // ESC { Until the escape key is pressed }
  165. }
  166.  
  167. void main()
  168. {
  169.   randomize();
  170.   SetMCGA();
  171.   Init();
  172.   Play();
  173.   SetText();
  174. }
  175.  
  176. /*
  177. BEGIN
  178.   clrscr;
  179.   writeln ('Hello! Another effect for you, this one is on starfields, again by');
  180.   writeln ('request.  In this sample program, a starfield will be coming towards');
  181.   writeln ('you. Hit the space bar to have it move away from you, any other key');
  182.   writeln ('to have it come towards you again. Hit [ESC] to end.');
  183.   writeln;
  184.   Writeln ('The logo at the top of the screen was drawn by me in Autodesk Animator.');
  185.   Writeln ('It only took a few seconds, so please don''t laugh too much at my attempt.');
  186.   writeln;
  187.   writeln ('The code is very easy to follow, and the documentation is as usual in the');
  188.   writeln ('main text. Leave me mail with further ideas for future trainers.');
  189.   writeln;
  190.   writeln;
  191.   write ('Hit any key to continue ...');
  192.   readkey;
  193.   randomize;
  194.   setmcga;
  195.   init;
  196.   Play;
  197.   settext;
  198.   Writeln ('All done. This concludes the thirteenth sample program in the ASPHYXIA');
  199.   Writeln ('Training series. You may reach DENTHOR under the names of GRANT');
  200.   Writeln ('SMITH/DENTHOR/ASPHYXIA on the ASPHYXIA BBS. I am also an avid');
  201.   Writeln ('Connectix BBS user, and occasionally read RSAProg. E-mail me at :');
  202.   Writeln ('    smith9@batis.bis.und.ac.za');
  203.   Writeln ('The numbers are available in the main text. You may also write to me at:');
  204.   Writeln ('             Grant Smith');
  205.   Writeln ('             P.O. Box 270');
  206.   Writeln ('             Kloof');
  207.   Writeln ('             3640');
  208.   Writeln ('             Natal');
  209.   Writeln ('             South Africa');
  210.   Writeln ('I hope to hear from you soon!');
  211.   Writeln; Writeln;
  212.   Write   ('Hit any key to exit ...');
  213.   readkey;
  214. END.
  215. */
  216.